package com.wssnail.controller;

import com.wssnail.service.VerifyUrlService;
import com.wssnail.service.WxService;
import com.wssnail.service.impl.VerifyUrlServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author 熟透的蜗牛
 * @version 1.0
 * @description: 校验连接的controller, 配置的路径和发送消息是同一个接口
 * @date 2022/10/9 21:59
 */
@RestController
@Slf4j
public class WxController {
    @Autowired
    private VerifyUrlService verifyUrlService;
    @Autowired
    private WxService wxService;

    /**
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @description: 校验链接
     * @return: java.lang.String
     * @author 熟透的蜗牛
     * @date: 2022/10/9 22:00
     */
    @GetMapping(value = "/xxx/onWxMsg")
    public String checkWxToken(@RequestParam(value = "signature", required = true) String signature,
                               @RequestParam(value = "timestamp", required = true) String timestamp,
                               @RequestParam(value = "nonce", required = true) String nonce,
                               @RequestParam(value = "echostr", required = true) String echostr) throws Exception {
        if (StringUtils.isNotEmpty(echostr)) {
            // 用于微信校验接口存在性，必须返回echostr
            return verifyUrlService.verifyUrl(signature, timestamp, nonce, echostr);
        }
        //返回echostr,或则success字符串，否则微信公众号识别不出来
        return "success";
    }

    /**
     * @description: 获取access_token, 注意服务器的ip一定要加加入白名单，否则获取失败，每天获取次数有限，请自行保存这个token
     * @return: java.lang.String
     * @author 熟透的蜗牛
     * @date: 2022/10/9 22:01
     */
    @GetMapping("/getAccessToken")
    public String getAccessToken() {
        // https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        return wxService.getToken();
    }
}
